/*
 * @Descripttion: 
 * @Author: chendingfengmail@163.com
 * @version: 
 * @Date: 2021-06-16 17:07:15
 * @LastEditors: chendingfengmail@163.com
 * @LastEditTime: 2021-12-29 10:23:56
 */
import api from '@/api'
import RegionData from '@/commons/region/regionData.js'
import util from '@/libs/util'
export default {
  namespaced: true,
  state: {
    // 区域数据(树结构)
    regionDatas: [],
    // 区域数据（列表结构）
    regionList: [],
    // 打开的树节点
    openNode: ""
  },
  mutations: {
    // 设置区域树打开的节点
    SET_OPEN_NODE(state, openNode) {
      state.openNode = openNode;
    },

    // 设置区域数据(树结构)
    SET_REGION_DATAS(state, region) {
      state.regionDatas = region;
    },
    // 设置区域数据(列表结构)
    SET_REGIONLIST_DATAS(state, regionList) {
      state.regionList = regionList;
    },
  },
  actions: {

    // 查询指定节点子区域列表信息
    async loadRegionChilds({ state, dispatch }, nodeCode) {
      return await new Promise((resolve, reject) => {
        api.region.findRegionChilds(nodeCode).then((res) => {
          resolve(res.value);
        }).catch((error) => {
          console.log(error)
          reject(error)
        })
      })
    },

    /**
     * @description 设置区域数据（一维数组）
     * @param {Object} context
     * @param {*} region region
     */
    async setRegionList({ state, commit, dispatch }, regionList) {
      // store 赋值
      commit("SET_REGIONLIST_DATAS", regionList);
      // 持久化
      await dispatch('ddAdmin/db/set', {
        dbName: 'zhgd',
        path: 'regionList',
        value: regionList,
        user: true
      }, { root: true })

    },
    /**
     * @description 从数据库取区域数据（一维数组）
     * @param {Object} context
     */
    async loadRegionList({ state, dispatch }) {
      state.regionList = RegionData.regionList
    },

    /**
     * @description 设置区域数据
     * @param {Object} context
     * @param {*} region region
     */
    async setRegion({ state, commit, dispatch }) {
      // store 赋值
      commit("SET_REGION_DATAS", RegionData.regionTree);

    },
    /**
     * @description 从数据库取区域数据
     * @param {Object} context
     */
    async loadRegion({ state, dispatch }) {
      let root = util.region.root
      let result = util.region.findRegionTreeByCode(root.code)
      state.regionDatas = result
    },

    /**
     * @description 设置默认打开的节点
     * @param {Object} context
     * @param {*} openNode
     */
    async setOpenNode({ state, commit, dispatch }, openNode) {
      // store 赋值
      commit("SET_OPEN_NODE", openNode);
      // 持久化
      await dispatch('ddAdmin/db/set', {
        dbName: 'zhgd',
        path: 'openNode',
        value: openNode,
        user: true
      }, { root: true })
    },
    /**
     * @description 从数据库取当前打开节点
     * @param {Object} context
     */
    async loadOpenNode({ state, commit, dispatch }) {
      // store 赋值
      state.openNode = await dispatch('ddAdmin/db/get', {
        dbName: 'zhgd',
        path: 'openNode',
        defaultValue: "000000",
        user: true
      }, { root: true })

    }
  }
}
